library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.1.3
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v tibble  3.1.6     v dplyr   1.1.0
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## v purrr   0.3.4
## Warning: package 'dplyr' was built under R version 4.1.3
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
## read and understand data ##
time.df <- read.csv('C:/My stuff/Data Science 1/DataSci_project/function/coordinates_Dami.csv')
head(df)
##                                               
## 1 function (x, df1, df2, ncp, log = FALSE)    
## 2 {                                           
## 3     if (missing(ncp))                       
## 4         .Call(C_df, x, df1, df2, log)       
## 5     else .Call(C_dnf, x, df1, df2, ncp, log)
## 6 }
#looking at the column's info
str(time.df)
## 'data.frame':    10305 obs. of  253 variables:
##  $ X                        : int  0 1 2 3 4 5 6 7 8 9 ...
##  $ NOSE_X                   : num  0.132 0.131 0.13 0.13 0.128 ...
##  $ NOSE_Y                   : num  0.446 0.446 0.446 0.446 0.446 ...
##  $ NOSE_Z                   : num  0.034 0.0467 0.0398 0.0484 0.0374 ...
##  $ NOSE_V                   : num  0.997 0.996 0.996 0.996 0.996 ...
##  $ LEFT_EYE_INNER_X         : num  0.124 0.123 0.123 0.122 0.121 ...
##  $ LEFT_EYE_INNER_Y         : num  0.426 0.427 0.427 0.427 0.426 ...
##  $ LEFT_EYE_INNER_Z         : num  0.0148 0.0272 0.021 0.0293 0.0173 ...
##  $ LEFT_EYE_INNER_V         : num  0.988 0.987 0.986 0.985 0.985 ...
##  $ LEFT_EYE_X               : num  0.119 0.119 0.118 0.118 0.117 ...
##  $ LEFT_EYE_Y               : num  0.425 0.426 0.426 0.426 0.426 ...
##  $ LEFT_EYE_Z               : num  0.0146 0.027 0.0207 0.029 0.017 ...
##  $ LEFT_EYE_V               : num  0.989 0.988 0.988 0.987 0.987 ...
##  $ LEFT_EYE_OUTER_X         : num  0.115 0.114 0.114 0.113 0.112 ...
##  $ LEFT_EYE_OUTER_Y         : num  0.424 0.425 0.425 0.425 0.425 ...
##  $ LEFT_EYE_OUTER_Z         : num  0.0144 0.0268 0.0205 0.0289 0.0169 ...
##  $ LEFT_EYE_OUTER_V         : num  0.99 0.989 0.989 0.988 0.988 ...
##  $ RIGHT_EYE_INNER_X        : num  0.135 0.134 0.133 0.132 0.131 ...
##  $ RIGHT_EYE_INNER_Y        : num  0.43 0.43 0.43 0.43 0.429 ...
##  $ RIGHT_EYE_INNER_Z        : num  -0.00731 0.00547 -0.00136 0.00709 -0.00656 ...
##  $ RIGHT_EYE_INNER_V        : num  0.996 0.995 0.995 0.995 0.995 ...
##  $ RIGHT_EYE_X              : num  0.138 0.136 0.135 0.134 0.133 ...
##  $ RIGHT_EYE_Y              : num  0.431 0.431 0.43 0.43 0.429 ...
##  $ RIGHT_EYE_Z              : num  -0.0076 0.00515 -0.00168 0.00676 -0.00689 ...
##  $ RIGHT_EYE_V              : num  0.997 0.997 0.997 0.997 0.997 ...
##  $ RIGHT_EYE_OUTER_X        : num  0.14 0.139 0.138 0.137 0.136 ...
##  $ RIGHT_EYE_OUTER_Y        : num  0.431 0.431 0.43 0.43 0.429 ...
##  $ RIGHT_EYE_OUTER_Z        : num  -0.00775 0.00498 -0.00187 0.00657 -0.0071 ...
##  $ RIGHT_EYE_OUTER_V        : num  0.998 0.998 0.998 0.998 0.998 ...
##  $ LEFT_EAR_X               : num  0.104 0.104 0.104 0.104 0.103 ...
##  $ LEFT_EAR_Y               : num  0.438 0.438 0.438 0.438 0.438 ...
##  $ LEFT_EAR_Z               : num  -0.0567 -0.0481 -0.0527 -0.0449 -0.0578 ...
##  $ LEFT_EAR_V               : num  0.998 0.997 0.997 0.997 0.997 ...
##  $ RIGHT_EAR_X              : num  0.138 0.138 0.137 0.137 0.137 ...
##  $ RIGHT_EAR_Y              : num  0.441 0.441 0.441 0.441 0.44 ...
##  $ RIGHT_EAR_Z              : num  -0.156 -0.146 -0.153 -0.144 -0.165 ...
##  $ RIGHT_EAR_V              : num  0.999 0.998 0.998 0.998 0.998 ...
##  $ MOUTH_LEFT_X             : num  0.125 0.125 0.125 0.125 0.125 ...
##  $ MOUTH_LEFT_Y             : num  0.468 0.468 0.468 0.468 0.467 ...
##  $ MOUTH_LEFT_Z             : num  0.0178 0.0292 0.0234 0.0317 0.0211 ...
##  $ MOUTH_LEFT_V             : num  0.997 0.997 0.997 0.996 0.997 ...
##  $ MOUTH_RIGHT_X            : num  0.135 0.134 0.134 0.133 0.132 ...
##  $ MOUTH_RIGHT_Y            : num  0.468 0.469 0.469 0.469 0.468 ...
##  $ MOUTH_RIGHT_Z            : num  -0.01245 -0.000699 -0.007381 0.001159 -0.011676 ...
##  $ MOUTH_RIGHT_V            : num  0.999 0.998 0.998 0.998 0.998 ...
##  $ LEFT_SHOULDER_X          : num  0.062 0.0634 0.0644 0.0647 0.0653 ...
##  $ LEFT_SHOULDER_Y          : num  0.557 0.557 0.557 0.559 0.559 ...
##  $ LEFT_SHOULDER_Z          : num  -0.02025 -0.01228 -0.01303 -0.00495 -0.00676 ...
##  $ LEFT_SHOULDER_V          : num  1 1 1 1 1 ...
##  $ RIGHT_SHOULDER_X         : num  0.127 0.126 0.125 0.124 0.123 ...
##  $ RIGHT_SHOULDER_Y         : num  0.593 0.593 0.594 0.595 0.595 ...
##  $ RIGHT_SHOULDER_Z         : num  -0.224 -0.222 -0.229 -0.225 -0.251 ...
##  $ RIGHT_SHOULDER_V         : num  1 1 1 1 1 ...
##  $ LEFT_ELBOW_X             : num  0.0722 0.0714 0.0717 0.0717 0.0714 ...
##  $ LEFT_ELBOW_Y             : num  0.693 0.701 0.701 0.703 0.703 ...
##  $ LEFT_ELBOW_Z             : num  0.159 0.179 0.185 0.192 0.206 ...
##  $ LEFT_ELBOW_V             : num  0.334 0.316 0.304 0.29 0.282 ...
##  $ RIGHT_ELBOW_X            : num  0.137 0.137 0.137 0.137 0.137 ...
##  $ RIGHT_ELBOW_Y            : num  0.791 0.789 0.792 0.794 0.795 ...
##  $ RIGHT_ELBOW_Z            : num  -0.219 -0.242 -0.247 -0.245 -0.272 ...
##  $ RIGHT_ELBOW_V            : num  0.936 0.931 0.929 0.926 0.926 ...
##  $ LEFT_WRIST_X             : num  0.139 0.136 0.136 0.136 0.135 ...
##  $ LEFT_WRIST_Y             : num  0.716 0.731 0.73 0.73 0.728 ...
##  $ LEFT_WRIST_Z             : num  0.349 0.391 0.408 0.408 0.437 ...
##  $ LEFT_WRIST_V             : num  0.144 0.138 0.137 0.134 0.133 ...
##  $ RIGHT_WRIST_X            : num  0.19 0.191 0.191 0.19 0.191 ...
##  $ RIGHT_WRIST_Y            : num  0.925 0.925 0.925 0.926 0.926 ...
##  $ RIGHT_WRIST_Z            : num  -4.71e-08 -1.84e-08 -2.61e-08 -2.19e-08 -1.38e-08 ...
##  $ RIGHT_WRIST_V            : num  0.994 0.993 0.993 0.993 0.993 ...
##  $ LEFT_PINKY_X             : num  0.155 0.15 0.15 0.149 0.149 ...
##  $ LEFT_PINKY_Y             : num  0.715 0.732 0.73 0.728 0.726 ...
##  $ LEFT_PINKY_Z             : num  0.381 0.427 0.446 0.446 0.478 ...
##  $ LEFT_PINKY_V             : num  0.233 0.225 0.229 0.228 0.228 ...
##  $ RIGHT_PINKY_X            : num  0.196 0.194 0.194 0.194 0.194 ...
##  $ RIGHT_PINKY_Y            : num  0.967 0.967 0.967 0.967 0.967 ...
##  $ RIGHT_PINKY_Z            : num  -0.163 -0.23 -0.218 -0.222 -0.237 ...
##  $ RIGHT_PINKY_V            : num  0.992 0.991 0.992 0.992 0.992 ...
##  $ LEFT_INDEX_X             : num  0.151 0.151 0.151 0.151 0.152 ...
##  $ LEFT_INDEX_Y             : num  0.697 0.72 0.717 0.717 0.715 ...
##  $ LEFT_INDEX_Z             : num  0.354 0.4 0.418 0.418 0.448 ...
##  $ LEFT_INDEX_V             : num  0.251 0.243 0.247 0.247 0.247 ...
##  $ RIGHT_INDEX_X            : num  0.203 0.202 0.202 0.202 0.201 ...
##  $ RIGHT_INDEX_Y            : num  0.964 0.964 0.965 0.965 0.965 ...
##  $ RIGHT_INDEX_Z            : num  -0.162 -0.224 -0.211 -0.216 -0.228 ...
##  $ RIGHT_INDEX_V            : num  0.991 0.991 0.991 0.991 0.991 ...
##  $ LEFT_THUMB_X             : num  0.157 0.152 0.152 0.151 0.151 ...
##  $ LEFT_THUMB_Y             : num  0.711 0.725 0.723 0.719 0.717 ...
##  $ LEFT_THUMB_Z             : num  0.342 0.385 0.403 0.403 0.431 ...
##  $ LEFT_THUMB_V             : num  0.271 0.262 0.266 0.264 0.264 ...
##  $ RIGHT_THUMB_X            : num  0.201 0.201 0.2 0.2 0.2 ...
##  $ RIGHT_THUMB_Y            : num  0.947 0.949 0.95 0.95 0.951 ...
##  $ RIGHT_THUMB_Z            : num  -0.146 -0.204 -0.193 -0.197 -0.209 ...
##  $ RIGHT_THUMB_V            : num  0.986 0.986 0.986 0.986 0.986 ...
##  $ LEFT_HIP_X               : num  0.0712 0.0715 0.0736 0.0739 0.074 ...
##  $ LEFT_HIP_Y               : num  0.882 0.891 0.894 0.9 0.901 ...
##  $ LEFT_HIP_Z               : num  0.0557 0.0565 0.0583 0.0593 0.069 ...
##  $ LEFT_HIP_V               : num  0.993 0.991 0.989 0.987 0.986 ...
##  $ RIGHT_HIP_X              : num  0.112 0.112 0.112 0.112 0.113 ...
##  $ RIGHT_HIP_Y              : num  0.9 0.909 0.914 0.92 0.921 ...
##   [list output truncated]
#Convert hertz to second (1 second = 30 hertz) 

time.df2 <- time.df %>%
    mutate(t_sec = X/30)
#aha moment happend at the second 203 based on the video recording
#truncating the data to 60 seconds before and after the insight moment
#plotting X and Y of right shoulder motion

right_shoulder_plot <- ggplot(time.df2, aes(x = RIGHT_SHOULDER_X, y =  RIGHT_SHOULDER_Y)) +
    geom_point() +
    geom_path() +
    theme_classic()
right_shoulder_plot

#plotting shoulder's x axes over time

right_shoulder_x <- ggplot(time.df2, aes(x = X, y =  RIGHT_SHOULDER_X)) +
  geom_point() +
  geom_path() +
  geom_vline(xintercept = 203*30, linetype = "dashed", color = "red")+  
  
  xlab("time")+
  theme_classic()

right_shoulder_x

#plotting shoulder's y axes over time

right_shoulder_y <- ggplot(time.df2, aes(x = X, y =  RIGHT_SHOULDER_Y)) +
  geom_point() +
  geom_path() +
  geom_vline(xintercept = 203*30, linetype = "dashed", color = "red") +
  xlab("time")+
  
  theme_classic()
right_shoulder_y

#Principle Component Analysis

#preparing data for Priciple Component Analysis
shoulder.df = time.df2 %>% select(RIGHT_SHOULDER_X, RIGHT_SHOULDER_Y, RIGHT_SHOULDER_Z)
#shoulder.df
pca_shoulder = prcomp(shoulder.df, center = T, scale. = T)

summary(pca_shoulder)
## Importance of components:
##                          PC1    PC2    PC3
## Standard deviation     1.352 0.9101 0.5871
## Proportion of Variance 0.609 0.2761 0.1149
## Cumulative Proportion  0.609 0.8851 1.0000
#Extract principal components
pc_scores <- pca_shoulder$x  # Scores (coordinates of the data points in the new space)
pc_loadings <- pca_shoulder$rotation  # Loadings (coefficients of the original variables in the principal components)
#Visualizing the proportion of variance explained by each principal component
plot(cumsum(pca_shoulder$sdev^2) / sum(pca_shoulder$sdev^2), xlab = "Number of Principal Components", ylab = "Cumulative Proportion of Variance Explained", type = "b")

#based on the plot above by two components 90% of the variance can be explained 
#Reduce dimensions
num_components_to_keep <- 2  # Choosing the number of components you want to keep
reduced_data <- pc_scores[, 1:num_components_to_keep]

# The 'reduced_data' dataframe now contains the data with reduced dimensions
# Create a dataframe for the reduced data
reduced_data_df <- as.data.frame(reduced_data)

# Scatter plot of the reduced data
plot(reduced_data_df$PC1, reduced_data_df$PC2, 
     xlab = "Principal Component 1",
     ylab = "Principal Component 2",
     main = "Scatter Plot of Reduced Data")

#plotting a 3D plot of the data

plot_3D_shoulder <- plot_ly(
  data = shoulder.df,
  x = ~RIGHT_SHOULDER_X,
  y = ~RIGHT_SHOULDER_Y,
  z = ~RIGHT_SHOULDER_Z,
  #text = ~paste("Time: ", time),
  type = "scatter3d",
  mode = "markers"
)

# Customize the layout
layout <- list(
  scene = list(
    xaxis = list(title = 'X-axis'),
    yaxis = list(title = 'Y-axis'),
    zaxis = list(title = 'Z-axis')
  )
)

# Combine plot and layout
plot_3D_shoulder <- plot_3D_shoulder %>% layout(layout)

# Display the interactive plot
plot_3D_shoulder

Machine’s confidnece in capturing the right motion

#V stands for visibility of the body parts (the red line) and how confident the machine is in capturing the right motion.

TS_right_shoulder <- time.df2 %>%
    ggplot(aes(x= t_sec, y= RIGHT_SHOULDER_Y)) +
    geom_point() +
    geom_path() +
    geom_path(aes(y=RIGHT_SHOULDER_V), color="red") +
    geom_path(aes(y=RIGHT_WRIST_X), color="blue") +
    scale_x_continuous("time (sec.)", expand=c(0,0)) +
    theme_classic()
TS_right_shoulder